package core

import (
	"encoding/json"
	"sync"
	"time"
)

// User 用户信息接口
type User interface {
	GetID() int64
	GetUniqueID() string
	GetNickname() string
	GetAvatar() string
	GetSex() string
	GetSocketID() string
	GetIsOnline() int
	SetIsOnline(status int)
	GetIsFaker() int
	GetIsApp() int
	GetIsWx() int
	SetSocketID(socketID string)
	GetLastLoginTime() int64
	SetLastLoginTime(time int64)
	GetLastInnerIP() string
	SetLastInnerIP(ip string)
	GetScore() int
	SetScore(score int)
	GetBirthday() string
	SetBirthday(birthday string)
	GetDisabledEndTime() int64
	GetAppID() int64
}

// Message 消息接口
type Message interface {
	GetType() string
	GetId() string
	SetChannel(channelId string)
	GetTimestamp() int64
	GetNeedAck() bool
	GetData() interface{}
}

// Connection WebSocket连接接口
type Connection interface {
	GetUser() User
	GetChannelID() string
	GetUserID() string
	GetIP() string
	WriteMessage(data Message) error
	WriteByteMessage(data []byte) error
	Close() error
	IsConnected() bool
	SetConnected(connected bool)
	GetWaits() *Waits
	SetWaits(waits *Waits)
	GetSendNo() int64
	IncrementSendNo()
	Kick()
	Ping()
	GetRole() interface{}
	SetRole(role interface{})
	Pong()
	GetUniqueID() string
	SetReadDeadline(second int) error
}

type AckMessage struct {
	ID   string `json:"id"`
	Type string `json:"type"`
}

// Waits 等待确认的消息管理器
type Waits struct {
	waits sync.Map
}

// Push 添加等待确认的消息
func (w *Waits) Push(requestID string, data []byte) {
	w.waits.Store(requestID, data)
}

// Has 检查是否存在等待确认的消息
func (w *Waits) Has(requestID string) bool {
	_, exists := w.waits.Load(requestID)
	return exists
}

// Del 删除等待确认的消息
func (w *Waits) Del(requestID string) {
	w.waits.Delete(requestID)
}

// ReplyMessage 基础消息结构
type ReplyMessage struct {
	ChannelID string                 `json:"id"`
	Event     string                 `json:"event"`
	Data      map[string]interface{} `json:"data"`
}

// GetEvent 获取事件名
func (m *ReplyMessage) GetEvent() string {
	return m.Event
}

// GetData 获取消息数据
func (m *ReplyMessage) GetData() map[string]interface{} {
	return m.Data
}

// GetChannelID 获取频道ID
func (m *ReplyMessage) GetChannelID() string {
	return m.ChannelID
}

// SetEvent 设置事件名
func (m *ReplyMessage) SetEvent(event string) {
	m.Event = event
}

// SetData 设置消息数据
func (m *ReplyMessage) SetData(data map[string]interface{}) {
	m.Data = data
}

// SetChannelID 设置频道ID
func (m *ReplyMessage) SetChannelID(channelID string) {
	m.ChannelID = channelID
}

// HealthEvent 健康检查事件
type HealthEvent struct {
	Event string          `json:"event"`
	ID    string          `json:"id"`
	Body  HealthEventMath `json:"body"`
}

// HealthEventMath 健康检查数学计算
type HealthEventMath struct {
	Math float64 `json:"math"`
}

// OnlineStatus 在线状态更新
type OnlineStatus struct {
	UserID string `json:"userId"`
	Status int    `json:"status"`
}

// Statistics 统计信息
type Statistics struct {
	Total int64 `json:"total"`
	Boy   int64 `json:"boy"`
	Girl  int64 `json:"girl"`
	NoSex int64 `json:"noSex"`
}

// Config WebSocket配置
type Config struct {
	Host              string        `json:"host"`
	Port              int           `json:"port"`
	TokenName         string        `json:"tokenName"`
	ReadTimeout       time.Duration `json:"readTimeout"`
	WriteTimeout      time.Duration `json:"writeTimeout"`
	MaxHeaderBytes    int           `json:"maxHeaderBytes"`
	SharedCount       int64         `json:"sharedCount"`
	EnableCompression bool          `json:"enableCompression"`
	ReadBufferSize    int           `json:"readBufferSize"`
	WriteBufferSize   int           `json:"writeBufferSize"`
}

// DefaultConfig 默认配置
func DefaultConfig() *Config {
	return &Config{
		Host:              "127.0.0.1",
		Port:              14101,
		ReadTimeout:       2 * time.Second,
		WriteTimeout:      2 * time.Second,
		MaxHeaderBytes:    1 << 20,
		SharedCount:       1000,
		EnableCompression: true,
		ReadBufferSize:    4096,
		WriteBufferSize:   4096,
	}
}

// JsonEncode JSON编码
func JsonEncode(data interface{}) []byte {
	bytes, _ := json.Marshal(data)
	return bytes
}
